package cn.csc.module.shop.controller.admin.businessdic;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.businessdic.vo.*;
import cn.csc.module.shop.convert.businessdic.ShopBusinessDicConvert;
import cn.csc.module.shop.dal.dataobject.businessdic.ShopBusinessDicDO;
import cn.csc.module.shop.service.businessdic.ShopBusinessDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 企业经营范围字典数据")
@RestController
@RequestMapping("/shop/business-dic")
@Validated
public class ShopBusinessDicController {

    @Resource
    private ShopBusinessDicService businessDicService;

    @PostMapping("/create")
    @ApiOperation("创建企业经营范围字典数据")
    @PreAuthorize("@ss.hasPermission('shop:business-dic:create')")
    public CommonResult<BigInteger> createBusinessDic(@Valid @RequestBody ShopBusinessDicCreateReqVO createReqVO) {
        return success(businessDicService.createBusinessDic(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新企业经营范围字典数据")
    @PreAuthorize("@ss.hasPermission('shop:business-dic:update')")
    public CommonResult<Boolean> updateBusinessDic(@Valid @RequestBody ShopBusinessDicUpdateReqVO updateReqVO) {
        businessDicService.updateBusinessDic(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除企业经营范围字典数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:business-dic:delete')")
    public CommonResult<Boolean> deleteBusinessDic(@RequestParam("id") BigInteger id) {
        businessDicService.deleteBusinessDic(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得企业经营范围字典数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:business-dic:query')")
    public CommonResult<ShopBusinessDicRespVO> getBusinessDic(@RequestParam("id") BigInteger id) {
        ShopBusinessDicDO businessDic = businessDicService.getBusinessDic(id);
        return success(ShopBusinessDicConvert.INSTANCE.convert(businessDic));
    }

    @GetMapping("/list")
    @ApiOperation("获得企业经营范围字典数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:business-dic:query')")
    public CommonResult<List<ShopBusinessDicRespVO>> getBusinessDicList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopBusinessDicDO> list = businessDicService.getBusinessDicList(ids);
        return success(ShopBusinessDicConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listOfAll")
    @ApiOperation("获得企业经营范围字典数据列表")
    @PermitAll
    public CommonResult<List<ShopBusinessDicDO>> getBusinessDicListOfAll() {
        List<ShopBusinessDicDO> list = businessDicService.getBusinessDicListOfAll();
        return success(list);
    }

    @GetMapping("/page")
    @ApiOperation("获得企业经营范围字典数据分页")
    @PreAuthorize("@ss.hasPermission('shop:business-dic:query')")
    public CommonResult<PageResult<ShopBusinessDicRespVO>> getBusinessDicPage(@Valid ShopBusinessDicPageReqVO pageVO) {
        PageResult<ShopBusinessDicDO> pageResult = businessDicService.getBusinessDicPage(pageVO);
        return success(ShopBusinessDicConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出企业经营范围字典数据 Excel")
    @PreAuthorize("@ss.hasPermission('shop:business-dic:export')")
    @OperateLog(type = EXPORT)
    public void exportBusinessDicExcel(@Valid ShopBusinessDicExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopBusinessDicDO> list = businessDicService.getBusinessDicList(exportReqVO);
        // 导出 Excel
        List<ShopBusinessDicExcelVO> datas = ShopBusinessDicConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "企业经营范围字典数据.xls", "数据", ShopBusinessDicExcelVO.class, datas);
    }

}
